<?php
declare(strict_types=1);


namespace App\Marketing\Mapper;

use App\Marketing\Model\MarketingBlindBox;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 盲盒列表Mapper类
 */
class MarketingBlindBoxMapper extends AbstractMapper
{
    /**
     * @var MarketingBlindBox
     */
    public $model;

    public function assignModel()
    {
        $this->model = MarketingBlindBox::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 主键
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 创建者
        if (isset($params['created_by']) && filled($params['created_by'])) {
            $query->where('created_by', '=', $params['created_by']);
        }

        // 更新者
        if (isset($params['updated_by']) && filled($params['updated_by'])) {
            $query->where('updated_by', '=', $params['updated_by']);
        }

        // 
        if (isset($params['created_at']) && filled($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && filled($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        // 删除时间
        if (isset($params['deleted_at']) && filled($params['deleted_at']) && is_array($params['deleted_at']) && count($params['deleted_at']) == 2) {
            $query->whereBetween(
                'deleted_at',
                [ $params['deleted_at'][0], $params['deleted_at'][1] ]
            );
        }

        // 
        if (isset($params['name']) && filled($params['name'])) {
            $query->where('name', '=', $params['name']);
        }

        // 标题
        if (isset($params['title']) && filled($params['title'])) {
            $query->where('title', '=', $params['title']);
        }

        // icon
        if (isset($params['icon']) && filled($params['icon'])) {
            $query->where('icon', '=', $params['icon']);
        }

        // code
        if (isset($params['code']) && filled($params['code'])) {
            $query->where('code', '=', $params['code']);
        }

        // 排序
        if (isset($params['sort']) && filled($params['sort'])) {
            $query->where('sort', '=', $params['sort']);
        }

        // 1奖金2奖品
        if (isset($params['type']) && filled($params['type'])) {
            $query->where('type', '=', $params['type']);
        }

        // 奖池金额
        if (isset($params['pool']) && filled($params['pool'])) {
            $query->where('pool', '=', $params['pool']);
        }

        // 价格
        if (isset($params['price']) && filled($params['price'])) {
            $query->where('price', '=', $params['price']);
        }

        // 中奖概率
        if (isset($params['rate']) && filled($params['rate'])) {
            $query->where('rate', '=', $params['rate']);
        }

        // 配置
        if (isset($params['config']) && filled($params['config'])) {
            $query->where('config', '=', $params['config']);
        }

        return $query;
    }
}